/**
 * 深拷贝
 */

function deepClone(obj) {
    if (typeof obj !== 'object' || obj === null) {
        return obj
    } else if (obj instanceof Date) {
        return new Date(obj)
    } else if (obj instanceof RegExp) {
        return new RegExp(obj)
    }
    let newObj = Array.isArray(obj) ? [] : {}
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[key] = deepClone(obj[key])
        }
    }
    return newObj
}

let obj = {
    a: 1,
    b: [1, 2, 3],
    c: {
        d: 4
    }
}
let newObj = deepClone(obj)
console.log(newObj)
newObj.b.push(4)
console.log(obj)
console.log(newObj)
console.log('constructor', obj.constructor, [].constructor)